<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="showAddShopCate" class="add-btn"
        >添加分类</el-button
      >

      <!-- 表格部分 -->
      <tree-table
        :data="categoriesList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <template slot="isEffective" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color:lightgreen"
            v-if="!scope.row.cat_deleted"
          ></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <template slot="cateLevel" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="cateAddDel">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="resetAddForm"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <!-- 级联选择器 -->
          <el-cascader
            v-model="selectedKeys"
            :options="cateTwoLvList"
            :props="cascaderProps"
            @change="handleChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addShopCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  components: {},
  props: {},
  data() {
    return {
      categoriesList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isEffective'
        },
        {
          label: '排序',
          type: 'template',
          template: 'cateLevel'
        },
        {
          label: '排序',
          type: 'template',
          template: 'cateAddDel'
        }
      ],
      cateTwoLvList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      selectedKeys: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadCategoriesList()
  },
  mounted() {},
  methods: {
    async loadCategoriesList() {
      const { data } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      console.log(data.data)
      if (data.meta.status !== 200) {
        return this.$message.error('获取商品分类列表失败')
      }
      this.categoriesList = data.data.result
      this.total = data.data.total
    },
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.loadCategoriesList()
    },
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.loadCategoriesList()
    },
    async showAddShopCate() {
      const { data } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      console.log(data.data)
      if (data.meta.status !== 200) {
        return this.$message.error('获取商品分类列表失败')
      }
      console.log(data.data)
      this.cateTwoLvList = data.data
      this.addCateDialogVisible = true
    },
    handleChange(value) {
      this.selectedKeys = value
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ]
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      console.log(this.addCateForm)
    },
    resetAddForm() {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    addShopCate() {
      this.$refs.addCateFormRef.validate(async boolean => {
        if (!boolean) return
        const { data } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        if (data.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }

        this.$message.success('添加分类成功')
        this.loadCategoriesList()
        this.addCateDialogVisible = false
      })
    }
  }
}
</script>

<style scoped lang="less">
.add-btn {
  margin-bottom: 15px;
}
</style>
